## Chapter 7 (Support for Compromise)
## This was originally chapter 8, but was changed to chapter 7 late in the process 
## Winter 2023 

###########################
## Figure 7.1: Bar Chart ##
##########################

compromise <- ggplot(data=data) + 
  geom_bar(aes(x=(-1*w7impcomp) + 5, 
               y=100*..prop..,
               fill=as.factor(w7compexpcond1control2in3out)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage of Respondents")  + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("Not at \n All Important","Not Too \n Important",
                              "Somewhat \n Important","Very \n Important")) + 
  theme_bw() +   
  ggtitle("Importance of Compromise") + 
  scale_fill_grey(name="", 
                  labels=c("Control","In-Party Politicians","Out-Party Politicians"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "none",
        panel.grid = element_blank()) 

respect <- ggplot(data=data) + 
  geom_bar(aes(x=(-1*w7imprespect)+5, 
               y=100*..prop..,
               fill=as.factor(w7compexpcond1control2in3out)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage of Respondents")  + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("Not at \n All Important","Not Too \n Important",
                              "Somewhat \n Important","Very \n Important")) + 
  theme_bw() +   
  ggtitle("Importance of Respect") + 
  scale_fill_grey(name="", 
                  labels=c("Control","In-Party Politicians","Out-Party Politicians"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 

pdf(file = "Figures/Druckman_etal_Figure_7_1.pdf",
    height = 11,
    width =  8.5) 
print(ggarrange(compromise, 
          respect,
          ncol = 1, nrow = 2))  
dev.off() 

#######################################
## Figure 7.2: Animus and Compromise ##
#######################################

## Facet these, first creating a factor variable for the faceting 
data$compromise_label <- factor(data$w7compexpcond1control2in3out,
                                labels = c("Control",
                                           "In-Party \n Politicians",
                                           "Out-Party \n Politicians")) 

## make a 6-panel version 
pdf(file = "Figures/Druckman_etal_Figure_7_2.pdf",
    height = 8.5,
    width =  11) 
print(data %>% 
  filter(year == 4 & !is.na(compromise_label)) %>% 
  mutate(resp_graph = -1*w7imprespect + 5,
         comp_graph = -1*w7impcomp + 5 ) %>% 
  select(c(apstable,compromise_label,resp_graph,comp_graph)) %>% 
  pivot_longer(cols = c(resp_graph,comp_graph),
               names_to = "item",
               values_to = "eval") %>% 
  mutate(item_factor = factor(item, 
                              labels = c("Compromise","Respect"))) %>% 
  ggplot(aes(apstable, eval)) + 
  geom_smooth(color = "black", 
              method = "gam",
              se = T) + 
  scale_x_continuous(breaks = ap_quantiles, 
                     labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
  scale_y_continuous(breaks = c(1:4),
                     limits = c(1,4),
                     labels = c("Not at \n All Important","Not too \n Important",
                                "Somewhat \n Important","Very \n Important")) + 
  xlab("Animus") +
  ylab("Importance of Compromise/Respect") + 
  #ylim(c(0,1)) + 
  #ggtitle("The Importance of Compromise & Respect") + 
  labs(color = "") + 
  theme_bw() + 
  facet_grid(cols = vars(item_factor),
             rows = vars(compromise_label)) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "none", 
        panel.grid = element_blank())) 
dev.off() 

## Figure 7.3 is the NASA vignette given to respondents (from a Word file) 

##############################################
## Figure 7.4: Approval for NASA Compromise ## 
##############################################

## code up the experimental treatment to be in-party/out-party 
## 0 is in-party outcome 
## 1 is the even split 
## 2 is out-party outcome 
data <- data %>% 
  mutate(nasa_inout = case_when(
    nasaexpcond1dem2split3rep == 1 & dem == 1 ~ 0, 
    nasaexpcond1dem2split3rep == 3 & dem == 0 ~ 0, 
    nasaexpcond1dem2split3rep == 2 ~ 1, 
    nasaexpcond1dem2split3rep == 3 & dem == 1 ~ 2, 
    nasaexpcond1dem2split3rep == 1 & dem == 0 ~ 2))  

pdf(file ="Figures/Druckman_etal_Figure_7_4.pdf",
    height = 11,
    width = 8.5)    
print(data %>% 
  filter(partyid_w1 != 4 & partychange == 0 & year == 4) %>% 
ggplot(data=.) + 
  geom_bar(aes(x=(-1*nasaopposecut)+5, 
               y=100*..prop..,
               fill=as.factor(nasa_inout)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage of Respondents")  + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("Strongly \n Oppose","Somewhat \n Oppose",
                              "Somewhat \n Favor","Strongly \n Favor")) + 
  theme_bw() +   
  #ggtitle("Support for NASA Spending Cuts") + 
  scale_fill_grey(name="", 
                  labels=c("Favors In-Party","Equal Split","Favors Out-Party"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()))  
dev.off() 

################################################
## Figure 7.5: Animus & Spending Cut Approval ## 
################################################ 

## Faceted Graph 
data$nasa_factor <- factor(data$nasa_inout,
                           levels = c(0,1,2),
                           labels = c("Favors In-Party",
                                      "Even Split",
                                      "Favors Out-Party")) 

pdf(file = "Figures/Druckman_etal_Figure_7_5.pdf",
    height = 8.5,
    width = 11)
print(data %>% 
        mutate(graph_nasa = -1*nasaopposecut + 5) %>% 
        filter(partyid_w1 != 4 & partychange == 0 & year == 4 & !is.na(nasa_factor)) %>% 
        ggplot(aes(apstable, graph_nasa)) + 
        ## use method gam to match other graphs 
        geom_smooth(color = "black", 
                    method = "gam", se = T) + 
        scale_x_continuous(breaks = ap_quantiles, 
                           labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
        scale_y_continuous(breaks = c(1:4),
                           limits = c(1,4),
                           labels=c("Strongly \n Oppose","Somewhat \n Oppose",
                                    "Somewhat \n Favor","Strongly \n Favor")) + 
        xlab("Animus") +
        ylab("Support for Compromise") + 
        #ylim(c(0,1)) + 
        #ggtitle("Support for NASA Spending Cuts") + 
        labs(color = "") + 
        theme_bw() + 
        facet_grid(cols = vars(nasa_factor)) + 
        theme(plot.title = element_text(hjust=0.5),
              legend.position = "bottom", 
              panel.grid = element_blank()))  
dev.off() 

#########################################
## Figure 7.6: Ideal Level of Spending ## 
#########################################

## party trends are very similar, just fold
data <- data %>% 
  mutate(rs_nasa = case_when(
    dem == 1 ~ 300 - nasaammount,
    dem == 0 ~ nasaammount - 300)) 

pdf(file="Figures/Druckman_etal_figure_7_6.pdf",
    height=11,
    width = 8.5)
print(data %>%  
  filter(year == 4 & !is.na(nasa_factor)) %>% 
  ggplot(aes(apstable, rs_nasa)) + 
  geom_smooth(color = "black", 
              method = "gam", se = T) + 
  geom_hline(yintercept = 0, lty = 2) + 
  scale_x_continuous(breaks = ap_quantiles, 
                     labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
  xlab("Animus") +
  ylab("Relative Amount (in Millions) \n (Higher Values Favor Respondent's Party)") + 
  ylim(c(-100,100)) + 
  #ggtitle("Animus and Amount of Spending Cuts") + 
  facet_grid(cols = vars(nasa_factor)) + 
  theme_bw() + 
  theme(plot.title = element_text(hjust=0.5),
        panel.grid = element_blank())) 
dev.off() 


#####################################
## For Appendix: Regression Models ## 
#####################################

# ## john's re-scaled measure 
# data <- data %>% 
#   mutate(rs_amount = case_when( 
#     dem == 0 ~ abs(nasaammount - 400),
#     dem == 1 ~ abs(nasaammount - 200))) 
# 
# fig_8_6_top <- data %>% 
#   filter(year == 4) %>% 
#   filter(dem == 1) %>% 
#   ggplot(aes(apstable, nasaammount,
#              group = nasa_inout)) + 
#   geom_smooth(aes(linetype = as.factor(nasa_inout)), 
#               color = "black", se = F) + 
#   scale_linetype_manual(name = "",
#                         values = c("dashed","solid","dotted"), 
#                         labels = c("Favor In-Party","Equal Split","Favor Out-Party")) + 
#   scale_x_continuous(breaks = ap_quantiles, 
#                      labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
#   xlab("") +
#   ylab("") + 
#   ylim(c(200,400)) + 
#   ggtitle("Amount to Cut (Democrats)") + 
#   labs(color = "") + 
#   theme_bw() + 
#   theme(plot.title = element_text(hjust=0.5),
#         legend.position = "none", 
#         panel.grid = element_blank()) 
# 
# fig_8_6_bottom <- data %>% 
#   filter(year == 4) %>% 
#   filter(dem == 0) %>% 
#   ggplot(aes(apstable, nasaammount,
#              group = nasa_inout)) + 
#   geom_smooth(aes(linetype = as.factor(nasa_inout)), 
#               color = "black", se = F) + 
#   scale_linetype_manual(name = "",
#                         values = c("dashed","solid","dotted"), 
#                         labels = c("Favor In-Party","Equal Split","Favor Out-Party")) + 
#   scale_x_continuous(breaks = ap_quantiles, 
#                      labels = c("5th \n pct","10th \n pct","33rd \n pct","50th \n pct","67th \n pct", "90th \n pct","95th \n pct")) + 
#   xlab("Partisan Animus") +
#   ylab("") + 
#   ylim(c(200,400)) + 
#   ggtitle("Amount to Cut (Republicans)") + 
#   labs(color = "") + 
#   theme_bw() + 
#   theme(plot.title = element_text(hjust=0.5),
#         legend.position = "bottom", 
#         panel.grid = element_blank()) 
# 
# ### combine them into one plot 
# pdf(file = "Figures/Druckman_etal_figure_A8_1.pdf",
#     height = 11,
#     width =  8.5) 
# print(ggarrange(fig_8_6_top,
#           fig_8_6_bottom,
#           ncol = 1, nrow = 2))  
# dev.off() 

## Regression Models of this data 
## code DV so that higher values means more importance 
data$comp_abstract <- -1*data$w7impcomp + 5 
data$respect_abstract <- -1*data$w7imprespect + 5 
m0 <- lm(comp_abstract ~ as.factor(w7compexpcond1control2in3out)*apstable,
         data = data,
         subset = year == 4) 
m1 <- lm(respect_abstract ~ as.factor(w7compexpcond1control2in3out)*apstable,
         data = data,
         subset = year == 4) 

## control for partisan strength  
data$pid_strength <- abs(data$partyid_w1 - 4)
  
m2 <- lm(comp_abstract ~ as.factor(w7compexpcond1control2in3out)*apstable + as.factor(w7compexpcond1control2in3out)*pid_strength,
         data = data,
         subset = year == 4) 
m3 <- lm(respect_abstract ~ as.factor(w7compexpcond1control2in3out)*apstable + as.factor(w7compexpcond1control2in3out)*pid_strength,
         data = data,
         subset = year == 4) 

## Make this into a table  
models <- list()
models[["Compromise (1)"]] <- m0  
models[["Compromise (2)"]] <- m2 
models[["Respect (3)"]] <- m1 
models[["Respect (4)"]] <- m3 

## add the N 
rows <- tribble(~term, ~"Compromise (1)",~"Compromise (2)",~"Respect (3)",~"Respect (4)", 
                'N',length(m0$residuals),length(m2$residuals),length(m1$residuals),length(m3$residuals))
attr(rows,'position') <- 19  

## clean up the GOF output (just N & R-Squared)
gm <- modelsummary::gof_map 
gm$omit <- TRUE 
gm$omit[5] <- FALSE 

msummary(models,
         add_rows = rows, 
         coef_map = c("(Intercept)" = "Constant",
                      "apstable" = "Partisan Animosity",
                      "pid_strength" = "Partisan Strength",
                      "as.factor(w7compexpcond1control2in3out)2" = "In-Party Condition",
                      "as.factor(w7compexpcond1control2in3out)3" = "Out-Party Condition",
                      "as.factor(w7compexpcond1control2in3out)2:apstable" = "In-Party*Animosity",
                      "as.factor(w7compexpcond1control2in3out)3:apstable" = "Out-Party*Animosity",
                      "as.factor(w7compexpcond1control2in3out)2:pid_strength" = "In-Party*Partisan Strength",
                      "as.factor(w7compexpcond1control2in3out)3:pid_strength" = "Out-Party*Partisan Strength"), 
         gof_map = gm, 
         stars = T, 
         output =  "Tables/raw/table_A7_1.html") 

## Look at the NASA Outcomes 
## scale so that higher values mean more support 
data$nasa_support <- -1*data$nasaopposecut + 5 
## change 50-50 split to be the baseline category 
data$nasa_inout <- factor(data$nasa_inout,
                          levels = c(1,0,2))
m2 <- lm(nasa_support ~ nasa_inout*apstable,
         data = data, 
         subset = year == 4) 
m2a <- lm(nasa_support ~ nasa_inout*apstable*rep,
         data = data, 
         subset = year == 4)  
## control for partisan strength 
m2b <- lm(nasa_support ~ nasa_inout*apstable*rep + nasa_inout*pid_strength*rep,
          data = data, 
          subset = year == 4)  
## no real diffs here in terms of support 
m3 <- lm(rs_nasa ~ nasa_inout*apstable,
         data = data, 
         subset = year == 4)  
m4 <- lm(rs_nasa ~ nasa_inout*apstable*rep,
         data = data, 
         subset = year == 4)   
m4a <- lm(rs_nasa ~ nasa_inout*apstable*rep + nasa_inout*pid_strength*rep,
         data = data, 
         subset = year == 4)  

## for Rs, are in-party and out-party animus differentiable? 
linearHypothesis(m4, "nasa_inout2:apstable:rep = nasa_inout0:apstable:rep")
## no, p=0.39

## Make this into a table  
models <- list()
models[["Support \n (1)"]] <- m2  
models[["Support \n (2)"]] <- m2a 
models[["Support \n (3)"]] <- m2b 
models[["Amount \n (4)"]] <- m3  
models[["Amount \n (5)"]] <- m4 
models[["Amount \n (6)"]] <- m4a 

## add the N 
rows <- tribble(~term, ~"Support (1)",~"Support (2)",~"Support (3)",~"Amount (4)",~"Amount (5)",~"Amount (6)", 
                'N',length(m2$residuals),length(m2a$residuals),length(m2b$residuals),length(m3$residuals),
                length(m4$residuals),length(m4a$residuals))
attr(rows,'position') <- 34 

## clean up the GOF output (just N & R-Squared)
gm <- modelsummary::gof_map 
gm$omit <- TRUE 
gm$omit[5] <- FALSE 

msummary(models,
         add_rows = rows, 
         coef_map = c("(Intercept)" = "Constant",
                      "apstable" = "Partisan Animosity",
                      "pid_strength" = "Partisan Strength",
                      "nasa_inout0" = "In-Party Split",
                      "nasa_inout2" = "Out-Party Split",
                      "nasa_inout0:apstable" = "In-Party*Animosity",
                      "nasa_inout2:apstable" = "Out-Party*Animosity", 
                      "rep" = "Republican",
                      "nasa_inout0:rep" = "In-Party*Republican",
                      "nasa_inout2:rep" = "Out-Party*Republican",
                      "apstable*rep" = "Partisan Animus*Republican",
                      "nasa_inout0:pid_strength" = "In-Party*Partisan Strength",
                      "nasa_inout2:pid_strength" = "Out-Party*Partisan Strength",
                      "nasa_inout0:apstable:rep" = "In-Party*Animosity*Republican",
                      "nasa_inout2:apstable:rep" = "Out-Party*Animosity*Republican",
                      "nasa_inout0:rep:pid_strength" = "In-Party*Partisan Strength*Republican",
                      "nasa_inout2:rep:pid_strength" = "Out-Party*Partisan Strength*Republican"),
         gof_map = gm, 
         stars = T, 
         output =  "Tables/raw/table_A7_2.html") 